package com.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import com.base.ResultSetHandler;
import com.dao.UserDAO;
import com.util.db.DBUtil;
import com.util.db.JdbcTemplate;
import com.vo.User;

public class UserDaoImpl implements UserDAO {
	private JdbcTemplate jdbcTemplate;

	public UserDaoImpl() {
		jdbcTemplate = new JdbcTemplate();
	}

	public void save(User user) throws SQLException {
		String sql = "insert into trainadd(major,trainname,begintime,endtime,trainaim,traincontent,classhour,teachername)"
				+ "values(?,?,?,?,?,?,?,?)";
		QueryRunner qr = new QueryRunner();
		qr.update(DBUtil.getConnection(), sql, user.getMajor(),
				user.getTrainname(), user.getBegintime(), user.getEndtime(),
				user.getTrainaim(), user.getTraincontent(),
				user.getClasshour(), user.getTeachername());

	}
    public void save1(User user) throws SQLException{
    	String sql = "insert into loadon(user_name,password)"+"values(?,?)";
    	QueryRunner qr = new QueryRunner();
    	qr.update(DBUtil.getConnection(),sql,user.getUser_name(),user.getPassword());
    }
	public void update(User user) throws SQLException {
		String sql = "updata trainadd major=?,trainname=?,begintime=?,endtime=?,trainaim=?,traincontent=?,"
				+ "classhour=?,teachername=? where id=?";
		QueryRunner qr = new QueryRunner();
		qr.update(DBUtil.getConnection(), sql, user.getMajor(),
				user.getTrainname(), user.getBegintime(), user.getEndtime(),
				user.getTrainaim(), user.getTraincontent(),
				user.getClasshour(), user.getTeachername());
	}

	@Override
	public User findById(int id) throws SQLException {
		return null;
	}

	@Override
	public List<User> find() throws SQLException {
		return null;
	}

	public boolean find(String username, String password) throws SQLException {
		String sql = "select *from loadon where user_name=? and password=?";
		User user = (User) jdbcTemplate.query(sql, new ResultSetHandler() {

			@Override
			public Object doHandler(ResultSet rs) throws SQLException {
				User user = null;

				if (rs.next()) {

					user = new User(username, password);
					user.setUser_name(rs.getString("user_name"));
					user.setPassword(rs.getString("password"));
				}
				return user;
			}
		}, username, password);

		if (user != null) {
			return true;
		}

		return false;
	}

	@Override
	public int delete(int id) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

}
